package com.ncmmall.api.apis.mall.wechat;


import com.ncmmall.api.apis.service.Result;
import com.ncmmall.api.params.AppletCodeDTO;
import com.ncmmall.api.utils.WechatUtils;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 小程序码api
 *
 * @author likaikai
 * @date 2019-07-17 16:33:14
 */
@RestController
@RequestMapping(value = "/applet/code")
public class AppletCodeApi {

    private final static Logger log = LoggerFactory.getLogger(AppletCodeApi.class);

    @Value("${store.domain}")
    private String storeDomain;

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private WechatUtils wechatUtils;

    /**
     * 生成小程序码
     *
     * @param page
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    private Result generateQrcode(@RequestBody AppletCodeDTO params, HttpServletRequest request) {
        Result result = new Result();
        try {
            String imageName = wechatUtils.generateQrcode(params.getPage(), params.getScene());
            return result.ok().inject(ImmutableMap.of("codeUrl", storeDomain + "/code/" + imageName));
        } catch (Exception e) {
            log.error("[生成小程序码异常]", e);
            return result.fail().setMsg("网络请求异常");
        }
    }

}
